<?php

/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-30
 * Time: 下午2:15
 */
namespace V5;
class C_Home extends \V4\C_Home
{
    public $isActive = true;
    public $remark = '用户端首页';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {return 'require user id ';}
        if (!isset($req['userType'])) {return 'require user type';}
        if (!isset($req['k']) || !isset($req['t'])) {return 'data error';}
        if (!isset($req['enterpriseId'])) {return 'require enterprise id';}
        if (!isset($req['channelId'])) {return 'require channel id';}
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $req['enterpriseId'], $req['t'], $req['k']) == false) {
            return 'data illegal';
        }
        $enterprise_id = $req['enterpriseId']; //获取企业id
        $channel_id = $req['channelId'];
        $fliter_product_ids = array(0);
        try {
            //获取渠道下所有在售产品
            $tmp_product_lists = array();
            $product_channel_handler = new \Biz\ProductHandler();
            $product_channel_lists = $product_channel_handler->getProductChannelByChannelId($channel_id);
            foreach ($product_channel_lists as $product) {
                $tmp_product_lists[$product['product_id']]['id'] = $product['product_id'];
                $tmp_product_lists[$product['product_id']]['enterprise_id'] = $product['enterprise_id'];
                $tmp_product_lists[$product['product_id']]['brand_id'] = $product['brand_id'];
                $tmp_product_lists[$product['product_id']]['brandname'] = (string) $product['brand_name'];
                $tmp_product_lists[$product['product_id']]['parent_brand_name'] = is_null($product['parent_brand_name']) ? '' : $product['parent_brand_name'];
                $tmp_product_lists[$product['product_id']]['parent_brand_id'] = is_null($product['parent_brand_id']) ? '' : $product['parent_brand_id'];;
                $tmp_product_lists[$product['product_id']]['name'] = (string) $product['name'];
                $tmp_product_lists[$product['product_id']]['icon_url'] = self::fullImgUrl($product['icon_url']);
                $tmp_product_lists[$product['product_id']]['mass_packing'] = (string) $product['sell_unit_string'];
                $tmp_product_lists[$product['product_id']]['remark'] = (string) $product['remark'];
                $tmp_product_lists[$product['product_id']]['channel_price'] = (string)$product['price'];
                $tmp_product_lists[$product['product_id']]['sort_price'] = (float)$product['price'];

                $fliter_product_ids[] = $product['product_id']; //重新组装待处理的商品id
            }
            $product_sale_valume = $product_channel_handler->getSalesVolumeByProductIds($fliter_product_ids); // 得到销量信息
            $product_sku_list = $product_channel_handler->getOnsaleAttrByProductIdsAndChannelId($fliter_product_ids,$channel_id);// 得到sku信息
            \Biz\Market\Product::handelSkuListPriceByChannelIdAndEnterpriseId($product_sku_list,$channel_id,$enterprise_id); // 根据渠道id批量处理sku价格，与参与活动状态

            $result_product_list = array(); // 所有商品补全促销信息
            foreach ($tmp_product_lists as $product_id=>$item) {
                $result_product_list[$product_id] = $item;
                $result_product_list[$product_id]['sales_valume'] = isset($product_sale_valume[$product_id]) ? $product_sale_valume[$product_id] : 0;
                $result_product_list[$product_id]['sku_info'] = isset($product_sku_list[$product_id]) ? $product_sku_list[$product_id] : array();

                if (isset($product_sku_list[$product_id][0]['join_market_type']) ){
                    $result_product_list[$product_id]['activity_info'] = array(array('id'=>1)); // 只需标记参与活动即可

                } else {
                    $result_product_list[$product_id]['activity_info'] = array();
                }
            }

            $channel_all_product_with_activity = self::sortById($result_product_list);
            $result['news_product_list'] = array();
            $result['market_product_list'] = array();
            $result['product_list'] = array();
            foreach ($channel_all_product_with_activity as $product) {
                if (count($product['activity_info']) && count($result['market_product_list']) < 6) { // 补全6个促销商品
                    $result['market_product_list'][] = $product;
                }
                if(count($result['news_product_list']) < 6) { // 补全6个最新商品
                    $result['news_product_list'][] = $product;
                }
            }

            $all_product_with_activity_sort_by_sales_valume = self::sortBySalesValume($result_product_list);
            foreach ($all_product_with_activity_sort_by_sales_valume as $product) {
                if(count($result['product_list']) < 6) { // 补全6个企业热卖商品
                    $result['product_list'][] = $product;
                }
            }

            /*
             * 获取广告位
             * $type =4 广告位
             * */
            $market_biz = new \Biz\MarketHandler();
            $tabulation = new \Cls\Tabulation();
            $tabulation->setLimit(99999);
            $market_biz->getAdvertisementOrHotsiteByEnterpriseId($enterprise_id, $tabulation);
            $advertiseInfo = array();
            $hotBrandInfo = array();
            $promotionInfo = array();
            $advertisement_list = $tabulation->getData();
            if ($tabulation->getSuccessStatus() === true && !empty($advertisement_list)) {
                /**
                 * @var $market_section \Cls\Market\Section
                 */
                foreach ($advertisement_list as $market_section) {
                    $market_section_list = $market_section->getAdvertisementAndPromotionList();
                    if (!empty($market_section_list)) {
                        /**
                         * @var $market_actionPara \Cls\Market\Actionpara
                         */
                        $section_type = $market_section->getSectionType();
                        if ($section_type == \Cls\Market\Section::SECTION_ADVERTISEMENT) { // 广告\

                            foreach ($market_section_list as $market_actionPara) {
                                $para_value = $market_actionPara->getParaValue();
                                if (count($advertiseInfo) <4){
                                    $advertiseInfo[] = parent::getByActionId(array(
                                        'id' => $market_section->getId(),
                                        'title' => $market_section->getTitle(),
                                        'icon_url' => $market_section->getIcon(),
                                        'action_id' => $market_actionPara->getActionId(),
                                        'action_name' => $market_actionPara->getActionName(),
                                        'para_value' => $para_value,
                                    ));
                                }
                            }
                        } else if ($section_type == \Cls\Market\Section::SECTION_BRAND) { // 热销分类
                            foreach ($market_section_list as $market_actionPara) {
                                $para_value = $market_actionPara->getParaValue();
                                if(count($hotBrandInfo) < 5) {
                                    $hotBrandInfo[] = array(
                                        'id' => $market_section->getId(),
                                        'title' => $market_section->getTitle(),
                                        'icon_url' => $market_section->getIcon(),
                                        'action' => array(
                                            'id' => $market_actionPara->getActionId(),
                                            'name' => $market_actionPara->getActionName(),
                                            'parameter' => array(
                                                'title' => $market_section->getTitle(),
                                                'brandId' => $para_value,
                                            ),

                                        ),
                                    );
                                }
                            }
                        } else if ($section_type == \Cls\Market\Section::SECTION_PROMOTION) {// 促销位
                            foreach ($market_section_list as $market_actionPara) {
                                $para_value = $market_actionPara->getParaValue();
                                if(count($hotBrandInfo) < 3) {
                                    $promotionInfo[] = parent::getByActionId(array(
                                        'id' => $market_section->getId(),
                                        'title' => $market_section->getTitle(),
                                        'icon_url' => $market_section->getIcon(),
                                        'action_id' => $market_actionPara->getActionId(),
                                        'action_name' => $market_actionPara->getActionName(),
                                        'para_value' => $para_value,
                                    ));
                                }
                            }
                        }
                    }
                }
            }
            $result['middle_function_area'] = array_merge(self::getMiddleFunctionArea(),$hotBrandInfo);

            if (count($advertiseInfo) == 0) {  // 添加默认广告位
                $advertiseInfo = array(
                    array(
                        "id" => "1",
                        "title" => "鲜桥默认广告1",
                        "icon_url" => "http://image.freshqiao.com/files/default_ad.png",
                        "action" => array(
                            "id" => "5",
                            "name" => "无动作",
                        )
                    ),
                    array(
                        "id" => "2",
                        "title" => "鲜桥默认广告2",
                        "icon_url" => "http://image.freshqiao.com/files/default_ad2.png",
                        "action" => array(
                            "id" => "5",
                            "name" => "无动作",
                        )
                    ),
                );
            }
            $result['advertise_list'] = $advertiseInfo;
            $result['promotion_list'] = $promotionInfo;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return '';
    }

    protected static function getMiddleFunctionArea()
    {
        return array(
            array(
                'id' => 1,
                'title' => '企业风采',
                'icon_url' => 'http://image.freshqiao.com/files/qiye_icon2.png',
                'action' => array(
                    'id' => '6',
                    'name' => '企业风采',
                ),
            ),
            array(
                'id' => 2,
                'title' => '新品上架',
                'icon_url' => 'http://image.freshqiao.com/files/xinpin_icon2.png',
                'action' => array(
                    'id' => '7',
                    'name' => '新品上架',
                ),
            ),
            array(
                'id' => 3,
                'title' => '我的收藏',
                'icon_url' => 'http://image.freshqiao.com/files/shoucang_icon2.png',
                'action' => array(
                    'id' => '8',
                    'name' => '我的收藏',
                ),
            ),
            array(
                'id' => 4,
                'title' => '最近购买',
                'icon_url' => 'http://image.freshqiao.com/files/goumai-icon2.png',
                'action' => array(
                    'id' => '9',
                    'name' => '最近购买',
                ),
            ),
            array(
                'id' => 5,
                'title' => '所有商品',
                'icon_url' => 'http://image.freshqiao.com/files/quanbu-icon2.png',
                'action' => array(
                    'id' => '10',
                    'name' => '全部商品',
                ),
            ),
        );
    }


    /**
     * 补全图片信息
     * @param $icon_url
     * @return string
     */
    protected static function fullImgUrl($icon_url)
    {
        $image_domain = 'http://image.freshqiao.com';
        $image_url = $icon_url == '/' ? $image_domain . $icon_url : $image_domain . '/' . $icon_url;
        return $image_url;
    }

    /**
     *  // 按brand升序排序
     * @param $product_list
     * @return mixed
     */
    protected static function sortById($product_list)
    {
        $ids = array();
        foreach ($product_list as $product) {
            $ids[] = $product['id'];
        }
        array_multisort($ids, SORT_ASC, $product_list);
        return $product_list;
    }
    /**
     *  // 按brand升序排序
     * @param $product_list
     * @return mixed
     */
    protected static function sortBySalesValume($product_list)
    {
        $ids = array();
        foreach ($product_list as $product) {
            $ids[] = $product['sales_valume'];
        }
        array_multisort($ids, SORT_ASC, $product_list);
        return $product_list;
    }
}